/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProcFornecedor.java
 *
 * Created on 03/12/2009, 11:45:19
 */

package sigbar.view;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableModel;

/**
 *
 * @author hdhzero
 */
public class ProcFornecedor extends javax.swing.JDialog {

    /** Creates new form ProcFornecedor */
    public ProcFornecedor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tableFornecedores = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        textfieldCNPJ = new javax.swing.JTextField();
        textfieldNomeEmp = new javax.swing.JTextField();
        textfieldNomeFan = new javax.swing.JTextField();
        textfieldCPF = new javax.swing.JTextField();
        textfieldNomePes = new javax.swing.JTextField();
        textfieldFone = new javax.swing.JTextField();
        textfieldEmail = new javax.swing.JTextField();
        buttonBuscar = new javax.swing.JButton();
        checkCNPJ = new javax.swing.JCheckBox();
        checkNomeEmp = new javax.swing.JCheckBox();
        checkNomeFan = new javax.swing.JCheckBox();
        checkNomePes = new javax.swing.JCheckBox();
        checkFone = new javax.swing.JCheckBox();
        checkEmail = new javax.swing.JCheckBox();
        checkCPF = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Procurar Fornecedor");
        setResizable(false);

        tableFornecedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "CNPJ", "Nome da Empresa", "Nome Fantasia", "CPF", "Nome Pessoa", "Fones", "E-mails"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.Object.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableFornecedores);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Busca de Fornecedores");

        buttonBuscar.setText("Buscar");
        buttonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBuscarActionPerformed(evt);
            }
        });

        checkCNPJ.setText("CNPJ :");
        checkCNPJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkCNPJActionPerformed(evt);
            }
        });

        checkNomeEmp.setText("Nome de Empresa :");

        checkNomeFan.setText("Nome Fantasia :");

        checkNomePes.setText("Nome Pessoa :");

        checkFone.setText("Fone :");

        checkEmail.setText("E-Mail :");

        checkCPF.setText("CPF :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(checkCNPJ)
                    .addComponent(checkNomePes))
                .addGap(2, 2, 2)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(textfieldNomePes, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkFone)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldFone, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkEmail)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkCPF)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(textfieldCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkNomeEmp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldNomeEmp, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(checkNomeFan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textfieldNomeFan, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(41, 41, 41)
                .addComponent(buttonBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkCNPJ)
                    .addComponent(textfieldCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkNomeEmp)
                    .addComponent(textfieldNomeEmp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkNomeFan)
                    .addComponent(textfieldNomeFan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(checkNomePes)
                    .addComponent(textfieldNomePes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkFone)
                    .addComponent(textfieldFone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkEmail)
                    .addComponent(textfieldEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(checkCPF)
                    .addComponent(textfieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(234, 234, 234))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(buttonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBuscarActionPerformed
        ListSelectionModel selecao = tableFornecedores.getSelectionModel();
        selecao.clearSelection();
        TableModel modelotab = tableFornecedores.getModel();
        boolean sair = false;
        ArrayList<Integer> selecionados = new ArrayList<Integer>();
        modelotab.setValueAt("555372", 0, 0);
        modelotab.setValueAt("117372", 1, 0);
        modelotab.setValueAt("998877", 2, 0);
        modelotab.setValueAt("Maricou", 0, 1);
        modelotab.setValueAt("Balela", 1, 1);
        modelotab.setValueAt("Blablou", 2, 1);
        modelotab.setValueAt("Mar ficou", 0, 2);
        modelotab.setValueAt("Bal no ela", 1, 2);
        modelotab.setValueAt("Bla tou blou", 2, 2);
        modelotab.setValueAt("99821", 0, 3);
        modelotab.setValueAt("00011", 1, 3);
        modelotab.setValueAt("88541", 2, 3);
        modelotab.setValueAt("Joazinho", 0, 4);
        modelotab.setValueAt("gabi", 1, 4);
        modelotab.setValueAt("anninha", 2, 4);
        modelotab.setValueAt("999-9999", 0, 5);
        modelotab.setValueAt("888-8888", 1, 5);
        modelotab.setValueAt("777-7777", 2, 5);
        modelotab.setValueAt("hotmail@hotmail.com", 0, 6);
        modelotab.setValueAt("yahoo@yahoo.com", 1, 6);
        modelotab.setValueAt("gmail@gmail.com", 2, 6);
        // Procurar pelo CNPJ
        if ( checkCNPJ.isSelected() ) {
            if ( !(textfieldCNPJ.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldCNPJ.getText().equals(modelotab.getValueAt(i, 0)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o CNPJ que deseja buscar");
            }
        }

        //Procurar pelo Nome de Empresa
        if ( checkNomeEmp.isSelected() && !sair ) {
            if ( !(textfieldNomeEmp.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldNomeEmp.getText().equals(modelotab.getValueAt(i, 1)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o Nome da Empresa que deseja buscar");
            }
        }

        //Procurar pelo Nome Fantasia
        if ( checkNomeFan.isSelected() && !sair) {
            if ( !(textfieldNomeFan.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldNomeFan.getText().equals(modelotab.getValueAt(i, 2)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o Nome Fantasia que deseja buscar");
            }
        }

        // Procurar pelo Nome Pessoa
        if ( checkNomePes.isSelected() && !sair) {
            if ( !(textfieldNomePes.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldNomePes.getText().equals(modelotab.getValueAt(i, 4)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o Nome da Pessoa que deseja buscar");
            }
        }

        //Procurar pelo Fone
        if ( checkFone.isSelected() && !sair) {
            if ( !(textfieldFone.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldFone.getText().equals(modelotab.getValueAt(i, 5)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o Telefone que deseja buscar");
            }
        }

        //Procurar pelo email
        if ( checkEmail.isSelected() && !sair) {
            if ( !(textfieldEmail.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldEmail.getText().equals(modelotab.getValueAt(i, 6)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
                if ( selecionados.size() == 0 ) {
                    sair = true;
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o E-mail que deseja buscar");
            }
        }

        //Procurar por CPF
        if ( checkCPF.isSelected() && !sair) {
            if ( !(textfieldCPF.getText().equals("")) ) {
                for (int i = 0; i < modelotab.getRowCount(); i++ ) {
                    if ( textfieldCPF.getText().equals(modelotab.getValueAt(i, 3)) ) {
                        if ( !selecionados.contains(i)) {
                            selecionados.add(i);
                        }
                    } else {
                        if ( selecionados.contains(i) ) {
                            selecionados.remove((Object)i);
                        }
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "Escreva o CPF que deseja buscar");
            }
        }

        // Selecionar Resultado na tabela
        if ( selecionados.size() == 0 ) {
            JOptionPane.showMessageDialog(this, "Não foi encontrada nenhum registro do Fornecedor procurado");
        } else {
            for (int i = 0; i < selecionados.size(); i++) {
                selecao.addSelectionInterval(selecionados.get(i), selecionados.get(i));
            }
        }
}//GEN-LAST:event_buttonBuscarActionPerformed

    private void checkCNPJActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkCNPJActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_checkCNPJActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBuscar;
    private javax.swing.JCheckBox checkCNPJ;
    private javax.swing.JCheckBox checkCPF;
    private javax.swing.JCheckBox checkEmail;
    private javax.swing.JCheckBox checkFone;
    private javax.swing.JCheckBox checkNomeEmp;
    private javax.swing.JCheckBox checkNomeFan;
    private javax.swing.JCheckBox checkNomePes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableFornecedores;
    private javax.swing.JTextField textfieldCNPJ;
    private javax.swing.JTextField textfieldCPF;
    private javax.swing.JTextField textfieldEmail;
    private javax.swing.JTextField textfieldFone;
    private javax.swing.JTextField textfieldNomeEmp;
    private javax.swing.JTextField textfieldNomeFan;
    private javax.swing.JTextField textfieldNomePes;
    // End of variables declaration//GEN-END:variables

}
